<template>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="作品名" prop="worktitle">
            <el-input v-model="ruleForm.worktitle"></el-input>
        </el-form-item>
        <el-form-item label="出镜" prop="modelname">
            <el-input v-model="ruleForm.modelname"></el-input>
        </el-form-item>
        <el-form-item label="摄影" prop="photographer">
            <el-input v-model="ruleForm.photographer"></el-input>
        </el-form-item>

        <el-form-item label="描述" prop="desc">
            <el-input type="textarea" v-model="ruleForm.desc"></el-input>
        </el-form-item>

        <el-tag
                :key="tag"
                v-for="tag in ruleForm.dynamicTags"
                closable
                :disable-transitions="false"
                @close="handleClose(tag)">
            {{tag}}
        </el-tag>

        <el-input
                class="input-new-tag"
                v-if="inputVisible"
                v-model="inputValue"
                ref="saveTagInput"
                size="small"
                @keyup.enter.native="handleInputConfirm"
                @blur="handleInputConfirm"
        >
        </el-input>
        <el-button v-else class="button-new-tag" size="small" @click="showInput">新增标签</el-button>

        <el-upload
                action="String"
                ref="upload"
                list-type="picture-card"
                multiple="multiple"
                :file-list="ruleForm.fileList"
                :auto-upload="false"
                :http-request="uploadFile"
                :on-change="handleChange"
                :on-preview="handlePictureCardPreview"
                :on-remove="handleRemove">
            <i class="el-icon-plus"></i>
        </el-upload>
        <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt="">
        </el-dialog>
        <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">立即发布</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
    </el-form>
</template>

<script>
    import axios from 'axios';
    export default {
        data() {
            return {
                userid:'',
                dialogImageUrl: '',
                dialogVisible: false,
                inputVisible: false,
                inputValue: '',
                multiple: true,
                formData: "",
                ruleForm: {
                    worktitle: '',
                    modelname: '',
                    photographer: '',
                    photodescribe: '',
                    desc:'',
                    fileList: [],
                    dynamicTags: []
                },
                rules: {
                    worktitle: [
                        { required: true, message: '请输入作品名称', trigger: 'blur' },
                    ],

                }
            };
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.formData= new FormData();
                        this.$refs.upload.submit();
                        this.formData.append("worktitle", this.ruleForm.worktitle);
                        this.formData.append("modelname", this.ruleForm.modelname);
                        this.formData.append("photographer", this.ruleForm.photographer);
                        this.formData.append("desc", this.ruleForm.desc);
                        this.formData.append("dynamicTags", this.ruleForm.dynamicTags);
                        let json=window.sessionStorage.getItem("user");
                        let user=JSON.parse(json);
                        this.userid=user.uUserid;
                        this.formData.append("userid",this.userid);
                        console.log(this.formData);
                            axios({
                                url:'/social/upload',
                                method:"post",
                                data:this.formData,
                                headers: {
                                    "Content-Type": "multipart/form-data;charset=utf-8"
                                }
                            }).then(ret=>{
                                if(ret.data=="success"){
                                    this.$message.success('发布登陆成功');
                                }
                            })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            uploadFile(file) {
                this.formData.append("files", file.file);
            },
            handleChange(file, fileList) {
                this.ruleForm.fileList = fileList;
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
                this.ruleForm.fileList=[];
                this.ruleForm.dynamicTags=[];
            },
            handleClose(tag) {
                this.ruleForm.dynamicTags.splice(this.ruleForm.dynamicTags.indexOf(tag), 1);
            },
            showInput() {
                this.inputVisible = true;
                this.$nextTick(_ => {
                    this.$refs.saveTagInput.$refs.input.focus();
                });
            },
            handleInputConfirm() {
                let inputValue = this.inputValue;
                if (inputValue) {
                    this.ruleForm.dynamicTags.push(inputValue);
                }
                this.inputVisible = false;
                this.inputValue = '';
            },
            handleRemove(file, fileList) {
                console.log(file, fileList);
            },
            handlePictureCardPreview(file) {
                this.dialogImageUrl = file.url;
                console.log(this.dialogImageUrl)
                this.dialogVisible = true;
            }
        }
    }
</script>

<style scoped>

    .el-tag + .el-tag {
        margin-left: 10px;
    }
    .button-new-tag {
        margin-left: 10px;
        height: 32px;
        line-height: 30px;
        padding-top: 0;
        padding-bottom: 0;
    }
    .input-new-tag {
        width: 90px;
        margin-left: 10px;
        vertical-align: bottom;
    }
</style>